package de.rts.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

import de.rts.logic.TrainScheduler;

public class RailwayLogIFrame extends JInternalFrame
{

  private AppendingTextPane pane;
  private TrainScheduler    scheduler;
  static int                logWidth      = RailwaySimulatorFrame.appWidth * 1 / 4 - 12;
  static int                logHeight     = RailwaySimulatorFrame.appHeight - RailwaySimulatorFrame.inset - 10;
  private Font              iceFont       = new Font("Arial", Font.PLAIN, 12);
  private Font              regionalFont  = new Font("Helvetica", Font.PLAIN, 12);
  private Font              cargoFont     = new Font("Times New Roman", Font.PLAIN, 13);
  private Font              defaultFont   = new Font("Courier", Font.BOLD, 14);
  private Color             regionalColor = Util.setHSBColor(262, 13, 38);
  private Color             cargoColor    = Util.setHSBColor(180, 43, 59);
  private Color             iceColor      = Util.setHSBColor(0, 0, 0);
  private Color             defaultColor  = Util.setHSBColor(160, 0, 25);

  public RailwayLogIFrame()
  {
    super("Log", true, false, true, true);
    setSize(logWidth, logHeight);
    setLocation(RailwayMapIFrame.mapWidth, 0);
    createJTextArea();
  }

  private void createJTextArea()
  {
    pane = new AppendingTextPane();
    pane.setFocusable(false);
    pane.setCaretPosition(0);
    pane.setMargin(new Insets(5, 5, 5, 5));
    JScrollPane scrollPane = new JScrollPane(pane);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    getContentPane().add(scrollPane);
  }

  public void setScheduler(TrainScheduler scheduler)
  {
    this.scheduler = scheduler;
    if (this.scheduler != null)
    {
      this.scheduler.addPropertyChangeListener(new PropertyChangeListener()
      {

        @Override
        public void propertyChange(PropertyChangeEvent e)
        {
          String message = (String) e.getNewValue();
          if (message.contains("RB"))
            pane.append(regionalColor, message, regionalFont);
          else if (message.contains("Cargo"))
            pane.append(cargoColor, message, cargoFont);
          else if (message.contains("ICE"))
            pane.append(iceColor, message, iceFont);
          else
            pane.append(defaultColor, message, defaultFont);
        }
      });
    }
  }

  public class AppendingTextPane extends JTextPane
  {
    public AppendingTextPane()
    {
      super();
    }

    public void append(Color c, String text, Font f)
    {
      StyleContext sc = StyleContext.getDefaultStyleContext();
      AttributeSet attribColor = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
      AttributeSet attribFontFamily = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.FontFamily,
        f.getFamily());
      AttributeSet attribFontSize = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.FontSize, f.getSize());
      setCaretPosition(getDocument().getLength());
      setCharacterAttributes(attribColor, false);
      setCharacterAttributes(attribFontFamily, false);
      setCharacterAttributes(attribFontSize, false);
      replaceSelection(text);
    }
  }

  protected void resetLog()
  {
    if (this.scheduler != null) this.scheduler.removeAllPropertyChangeListeners();
    pane.setText("");
  }
}
